package com.liuche.homechat.common.user.controller;

import com.liuche.homechat.common.common.model.dto.RocketMQConfigProperties;
import com.liuche.homechat.common.common.model.vo.resp.ApiResult;
import io.swagger.annotations.Api;
import javafx.scene.input.DataFormat;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author：liuche
 * @Date：2024年06月02日 14:53
 * @Description：
 */
@RestController
@RequestMapping("/mq")
@Api(tags = "小火箭")
@Slf4j
public class RocketMQTestController {
    public static final String topic = "a_topic";
    @Resource
    private RocketMQTemplate rocketmqTemplate;

    @Resource
    private RocketMQConfigProperties prop;

    @GetMapping("/test")
    public ApiResult<?> test(@RequestParam String message) {
        rocketmqTemplate.convertAndSend(topic, message);
        return ApiResult.success();
    }

    @GetMapping("/common")
    public String common(@RequestParam String message) {
        // 同步发送
        rocketmqTemplate.syncSend(prop.getTopic().get("common"), new GenericMessage<>("syncSend，我是个普通的延迟消息" + "->" + message + new Date()), 3000, 3);
        // 异步发送
        rocketmqTemplate.asyncSend(prop.getTopic().get("common"), "asyncSend" + "->" + message, new SendCallback() {
        @Override
        public void onSuccess(SendResult sendResult) {
            log.info("Message send successfully! MessageId: {}", sendResult.getMsgId());
        }
        @Override
        public void onException(Throwable e) {
            log.error("Failed to send message: {}", e.getMessage(), e);
        }
        });
        // 单程发送
        rocketmqTemplate.sendOneWay(prop.getTopic().get("common"), "sendOneWay" + "->" + message);
        return "ok";
    }

}
